import parole
from parole.colornames import colors
from parole.display import interpolateRGB
import pygame, random

import sim, main, sim_items
from util import *

class Potion(sim_items.Potion):
    def __init__(self):
        sim_items.Potion.__init__(self, "bioluminescence", "!",
                ('light', 'life', 'near'),
                idDescription="As you swirl the potion around in its flask, "\
                    "you notice faint bubbles of green light appear and "\
                    "disappear. These potions are particularly prized by "\
                    "miners and all others who toil away in the dark places "\
                    "of the world.")

    def quaff(self, quaffer):
        if quaffer is not main.player:
            # TODO: NPC message
            pass
        else:
            # PC message
            main.transient('Your whole body begins to glow eerily.')
            main.player.identify(self)

        self.quaffer = quaffer
        rgb = random.choice(['Lime', 'PaleTurquoise', 'Cyan', 'Pink',
            'SpringGreen', 'Teal'])
        self.lightSource = parole.map.LightSource(colors[rgb], 8.0)
        self.lightSource.apply(self.quaffer.parentTile.map, self.quaffer.pos)
        self.quaffer.parentTile.map.update()

        if main.mapFrame.fovObj and not main.player.dead:
            main.mapFrame.touchFOV()

        self.expired = False
        self.duration = 10*60000

        main.schedule.add(sim.Event('expire effect', main.schedule.time +\
            self.duration,
            (self,), dests=[self]))
        main.schedule(sim.Event('statusNote+', main.schedule.time, (quaffer,
            'biolum'), dests=[quaffer]))
        main.schedule(sim.Event('statusNote-', main.schedule.time +\
            self.duration, (quaffer, 'biolum'), dests=[quaffer]))
        main.schedule.listen('enter tile', self)
        main.schedule.listen('leave tile', self)

    def listen(self, event):
        sim_items.Potion.listen(self, event)

        if event.id == 'expire effect' and event.args[0] is self:
            self.expired = True
            if self.quaffer is main.player:
                # PC message
                main.transient('You stop glowing.')
            else:
                # TODO: NPC message
                pass
            main.schedule.unlisten('enter tile', self)
            main.schedule.unlisten('leave tile', self)
            self.lightSource.remove(self.quaffer.parentTile.map)
            if main.mapFrame.fovObj and not main.player.dead:
                main.mapFrame.touchFOV()

        elif event.id == 'leave tile' and not self.expired:
            obj, pos = event.args
            if obj is self.quaffer:
                self.lightSource.remove(self.quaffer.parentTile.map)
                if main.mapFrame.fovObj and not main.player.dead:
                    main.mapFrame.touchFOV()

        elif event.id == 'enter tile' and not self.expired:
            obj, epos, emap = event.args
            if obj is self.quaffer:
                self.lightSource.apply(self.quaffer.parentTile.map,
                        self.quaffer.pos)
                self.quaffer.parentTile.map.update()

                if main.mapFrame.fovObj and not main.player.dead:
                    main.mapFrame.touchFOV()


#========================================
thingClass = Potion

